package com.zjdiepu.www.rest.api.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.handler.TokenHelper;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.ibiz.base.ISysUserService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.base.request.LoginRequest;
import com.zjdiepu.www.util.PasswordUtil;

/**
 * 项目名称：diepu-parent-rest <br/> 
 * 类名称：LoginController <br/>  
 * 类描述：登录控制层 <br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年6月5日 上午11:40:39  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年6月5日 上午11:40:39   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@RestController
@RequestMapping(value = "LoginController", produces="application/json;charset=UTF-8",method = RequestMethod.POST)
@Api(tags = {"登录/登出"}, produces="application/json;charset=UTF-8")
public class LoginController extends BaseController {
	@Resource
	private TokenHelper tokenHelper;
	@Resource
	private TokenUserHelper tokenUserHelper;
	@Resource
	private ISysUserService sysUserService;
	
	/**
     * @author xiongxiaotun <br/>
     * @Title: login  <br/>
     * @Description: 登录<br/>
     * @param sysUser
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "登录", response = Map.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "login")
	public ApiResponse login(@Validated @RequestBody LoginRequest loginRequest, HttpServletRequest request){
		Map<String, Object> result = CollectionInitUtil.emptyMap();
		String loginName = loginRequest.getLoginName();
		//到数据库查是否有此对象
        SysUser loginUser= sysUserService.selectByName(loginName, true);
        if(loginUser == null){
        	return ApiResponse.fail(999, "登录账号或密码错误");
        }
    	String loginPass = loginUser.getLoginPass();
    	if(!PasswordUtil.getDbPasswordForMd5(loginRequest.getLoginName(), loginRequest.getLoginPass()).equals(loginPass)) {
    		return ApiResponse.fail(999, "登录账号或密码错误");	
    	}
    	Integer orgDpStatus = loginUser.getOrgDpStatus();
    	if(!DpStatus.NORMAL.getCode().equals(orgDpStatus)) {
    		return ApiResponse.fail(999, "账号所在组织机构状态不正常");	
    	}
    	
    	String salersmanId = loginUser.getSalersmanId();    	
    	if(StringUtils.isBlank(salersmanId)) {
    		return ApiResponse.fail(999, "只有业务员能够负责客户接洽");
    	}
    	String token = tokenHelper.generateToken(loginUser.getId(), loginUser.getLoginName());
    	loginUser.setDevicetoken(loginRequest.getDevicetoken()); //设备标识
    	loginUser.setDeviceType(request.getHeader(SystemConsts.MOBILE_FLAG));
    	tokenUserHelper.cacheUser(token, loginUser);
    	TokenHelper.setToken(token); //线程变量
    	result.put("token", token);
    	result.put("user", loginUser);
		return ApiResponse.success(result);
	}
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: logout  <br/>
     * @Description: 登出<br/>
     * @param sysUser
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "登出", response = Boolean.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "logout")
	public ApiResponse logout(){
    	tokenUserHelper.delUser();
    	return ApiResponse.success(true);
	}
}
